package com.reportgenerate.marketreport.exception.handler;

import com.reportgenerate.marketreport.exception.DeleteObjectException;
import com.reportgenerate.marketreport.exception.ErrorData;
import com.reportgenerate.marketreport.exception.SaveObjectException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * @program: reportgenerate
 * @Description: 自定义异常处理器
 * @author: Mr.Cheng
 * @Email: ccelephant_518@126.com
 * @date: 2023/8/21 17:47
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(SaveObjectException.class)
    public ResponseEntity<ErrorData> handleCustomException(SaveObjectException ex) {
        log.info("internal exception: {}", ex.getMessage());
        String objectName = ex.getObjectName();
        ErrorData error = new ErrorData("保存" + objectName + "失败");
        error.setCode("400");
        return new ResponseEntity<ErrorData>(error, HttpStatus.BAD_REQUEST);
    }

    @ExceptionHandler(DeleteObjectException.class)
    public ResponseEntity<ErrorData> handleCustomException(DeleteObjectException ex) {
        log.info("internal exception: {}", ex.getMessage());
        String objectName = ex.getObjectName();
        ErrorData error = new ErrorData("删除" + objectName + "失败");
        error.setCode("401");
        return new ResponseEntity<ErrorData>(error, HttpStatus.BAD_REQUEST);
    }
}
